"""
读取文件内容
"""
file_path = "happy.txt"


def read_file(file_path):
    try:
        with open(file_path, mode='r', encoding='utf-8') as file:
            content=file.read()
        return content
    except FileNotFoundError:
        print(f'{file_path}文件未找到')


if __name__ == '__main__':
    print(read_file(file_path))
"""
写文件的方法
"""
class TxtFileWrite:
    def __init__(self, file_path ):
        self.file_path = file_path  # 存储文件路径

    def write_file(self,content):
        try:
            # 打开文件，'r' 表示只读模式
            with open(self.file_path, 'w',encoding='utf-8') as file:
                # 写入文件的全部内容
                file.write(content)
            print(f'成功写入文件{self.file_path}')
        except FileNotFoundError:  # 处理文件未找到的异常
            print(f"文件 {self.file_path} 未找到，请检查文件路径是否正确。")
        except Exception as e:  # 处理其他异常
            print(f"读取文件时出现错误: {e}")


if __name__ == '__main__':
    write = TxtFileWrite("write.txt")
    write.write_file("这是要写入的内容21435436547ertyuioigfhj")
